package com.wf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wf.context.BaseContext;
import com.wf.domain.ContractInfo;
import com.wf.domain.PaymentManage;
import com.wf.domain.User;
import com.wf.dto.PayVO;
import com.wf.mapper.*;
import com.wf.service.ContractInfoService;
import com.wf.service.HousesInfoService;
import com.wf.service.PaymentManageService;
import com.wf.vo.HouseDetailsVO;
import com.wf.vo.PageResultVO;
import com.wf.vo.PaymentDetailsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author mpk16
 * @description 针对表【payment_manage】的数据库操作Service实现
 * @createDate 2024-08-03 13:39:49
 */
@Service
public class PaymentManageServiceImpl extends ServiceImpl<PaymentManageMapper, PaymentManage>
        implements PaymentManageService {
    @Autowired
    PaymentManageMapper paymentManageMapper;
    @Autowired
    PaymentRecordMapper paymentRecordMapper;
    @Autowired
    PropertyItemMapper propertyItemMapper;
    @Autowired
    HousesInfoService housesInfoService;
    @Autowired
    ContractInfoMapper contractInfoMapper;
    @Autowired
    UserMapper userMapper;



    @Override
    public PageResultVO<PaymentDetailsVO> unpaid() {

        PageResultVO<PaymentDetailsVO> pageResultVO = new PageResultVO<>();
        List<HouseDetailsVO> houseDetails = housesInfoService.getHouseByUserId(BaseContext.getCurrentId());
        if (houseDetails == null) {
            return pageResultVO;
        }
        List<PaymentDetailsVO> paymentDetails =new ArrayList<>();
        houseDetails.forEach(
                houseDetailsVO -> {
                    List<PaymentDetailsVO> detailsVOList = paymentManageMapper.unpaid(houseDetailsVO.getHousesId());
                    paymentDetails.addAll(detailsVOList.stream().map(
                            paymentDetailsVO -> {
                                paymentDetailsVO.setImages(houseDetailsVO.getImage());
                                paymentDetailsVO.setContractId(houseDetailsVO.getContractId());
                                return paymentDetailsVO;
                            }
                    ).collect(Collectors.toList()));
                }

        );

        pageResultVO.setList(paymentDetails);
        pageResultVO.setTotal(paymentDetails.size());

        return pageResultVO;

    }

    @Override
    public List<PayVO> selectAllPay(Long houses_id, Long user_id,Integer contract_id) {
        List<PayVO> payVOS = paymentManageMapper.selectAllPay(houses_id,user_id,contract_id);
        return payVOS;
    }

    @Override
    public List<PaymentManage> selectPayMess(Long houses_id) {
        return paymentManageMapper.selectPayMess(houses_id);
    }

    @Override
    public HashMap<User,List<String>> reminders() {
        HashMap<User, List<String>> userListHashMap = new HashMap<>();
        List<PaymentManage> paymentManages = paymentManageMapper.selectNoPay();
        System.out.println("paymentManages = " + paymentManages);
        for (PaymentManage p:paymentManages) {
            Integer housesId = p.getHousesId();
            QueryWrapper<ContractInfo> contractInfoQueryWrapper = new QueryWrapper<>();
            HashMap<String, Object> map = new HashMap<>();
            map.put("house_id",housesId);
            map.put("status",1);
            contractInfoQueryWrapper.allEq(map);
            System.out.println("开始查找合同信息");
            ContractInfo contractInfo = contractInfoMapper.selectOne(contractInfoQueryWrapper);
            Long userId = contractInfo.getUserId();
            User user = userMapper.selectById(userId);
            Set<User> users = userListHashMap.keySet();
            if (!users.contains(user)){
                userListHashMap.put(user,new ArrayList<>());
            }
            List<String> strings = userListHashMap.get(user);
            strings.add(p.getPropertyItem().getName()+":"+p.getDosage()*p.getPropertyItem().getPrice());
            System.out.println("strings = " + strings);
        }
        return userListHashMap;
    }

    @Override
    public boolean pullBill(HashMap<String, Object> map) {
        int count = 0;
        boolean flag = false;
        List<Map<String,Object>> list = (List<Map<String, Object>>) map.get("list");
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime date = LocalDateTime.parse((String) map.get("nowDate"),formatter);
        for (Map<String,Object> m:list) {
            PaymentManage paymentManage = new PaymentManage();
            if (m.get("type").equals(2)){
                paymentManage.setDosage(1);
            }else {
                paymentManage.setDosage(Integer.parseInt(m.get("number")+""));
            }
            paymentManage.setCreateDate(date);
            paymentManage.setHousesId(Integer.parseInt(map.get("houseId")+""));
            paymentManage.setYear(date.getYear());
            paymentManage.setMonth(date.getMonthValue());
            paymentManage.setType(Integer.parseInt(m.get("itemId")+""));
            paymentManage.setUpdateDate(date);
//            System.out.println("paymentManage = " + paymentManage);
            count += paymentManageMapper.insert(paymentManage);
        }
        if (count == list.size()){
            flag = true;
        }
        return flag;
    }

    @Override
    public PageResultVO<PaymentDetailsVO> paid() {
        PageResultVO<PaymentDetailsVO> pageResultVO = new PageResultVO<>();
        List<HouseDetailsVO> houseDetails = housesInfoService.getHouseByUserId(BaseContext.getCurrentId());
        if (houseDetails == null) {
            return pageResultVO;
        }
        List<PaymentDetailsVO> paymentDetails =new ArrayList<>();
        houseDetails.forEach(
                houseDetailsVO -> {
                    List<PaymentDetailsVO> detailsVOList = paymentManageMapper.paid(houseDetailsVO.getHousesId());
                    paymentDetails.addAll(detailsVOList.stream().map(
                            paymentDetailsVO -> {
                                paymentDetailsVO.setImages(houseDetailsVO.getImage());
                                paymentDetailsVO.setContractId(houseDetailsVO.getContractId());
                                return paymentDetailsVO;
                            }
                    ).collect(Collectors.toList()));
                }

        );

        pageResultVO.setList(paymentDetails);
        pageResultVO.setTotal(paymentDetails.size());

        return pageResultVO;
    }
}




